/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.servlet;

import com.kamus.Constants;
import com.kamus.manager.KosakataGaulMgr;
import com.kamus.manager.KosakataIndMgr;
import com.kamus.manager.PasanganKataGIMgr;
import com.kamus.manager.PasanganKataIGMgr;
import com.kamus.model.PasanganKata;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author omega
 */
public class KosakataServlet extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String action = request.getParameter(Constants.ACTION);

        if (action.equals(Constants.ADD)) {
            addProcess(request);
            RequestDispatcher rd = request.getRequestDispatcher("KosakataForm.jsp");
            rd.forward(request, response);
        } else if (action.equals(Constants.EDIT)) {
            String url = request.getParameter(Constants.TYPE).equals(Constants.GAUL_IND) ? "KosakataListGI.jsp" : "KosakataListIG.jsp";
            editProcess(request);
            RequestDispatcher rd = request.getRequestDispatcher(url.toString());
            rd.forward(request, response);
        }
    }

    private void editProcess(HttpServletRequest request) {
        String type = request.getParameter(Constants.TYPE);
        String id = request.getParameter(Constants.ID);
        String gaul = request.getParameter("gaul");
        String indonesia = request.getParameter("indonesia");

        PasanganKata pasKata = new PasanganKata();
        pasKata.setId(Integer.parseInt(id));
        pasKata.setGaul(gaul);
        pasKata.setIndonesia(indonesia);

        if (type.equals(Constants.GAUL_IND)) {
            PasanganKataGIMgr.getInstance().updateKata(pasKata);
        } else if (type.equals(Constants.IND_GAUL)) {
            PasanganKataIGMgr.getInstance().updateKata(pasKata);
        }
    }

    private void addProcess(HttpServletRequest request) {
        String orientation = request.getParameter("orientation");
        String gaul = request.getParameter("gaul");
        String indonesia = request.getParameter("indonesia");

        PasanganKata pasKata = new PasanganKata();
        pasKata.setGaul(gaul);
        pasKata.setIndonesia(indonesia);

        if (orientation.equals("0")) {
            PasanganKataGIMgr.getInstance().insertPasKata(pasKata);
        } else if (orientation.equals("1")) {
            PasanganKataIGMgr.getInstance().insertPasKata(pasKata);
        } else if (orientation.equals("2")) {
            PasanganKataIGMgr.getInstance().insertPasKata(pasKata);
            PasanganKataGIMgr.getInstance().insertPasKata(pasKata);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
